package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.third.SinoCamReceiveNestPart;
import com.zmj.sy.mom.srv.aps.bean.vo.plate.PlatePartListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.qms.PlatePartReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportProductionNestPartResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportProductionOrderNestResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportProductionPartNestResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.workshop.WorkshopPlatePartResVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SinoCamReceiveNestPartMapper extends SyMapper<SinoCamReceiveNestPart> {
    @Select("select\n" +
            "              t1.id,\n" +
            "              t4.construct_no, -- 施工号\n" +
            "              t4.order_code, -- 订单号\n" +
            "              t5.work_order_code, -- 工单号\n" +
            "              t1.part_id, -- 零件编码\n" +
            "              t1.part_name, -- 零件名称\n" +
            "              t5.drawing, -- 零件图\n" +
            "              null partLength, -- 零件长度\n" +
            "              null partWidth, -- 零件宽度\n" +
            "              t5.mater_high partHeight, -- 零件厚度\n" +
            "              t1.quantity partCount -- 数量\n" +
            "            from tb_third_sinocam_receive_nest_part t1\n" +
            "              inner join tb_third_sinocam_receive_nest_plate t2 on t1.receive_nest_id=t2.receive_nest_id\n" +
            "              inner join tb_aps_work_detail t3 on t1.nest_plan_id=t3.id\n" +
            "              inner join tb_order t4 on t3.order_id=t4.id\n" +
            "              inner join tb_aps_work_order t5 on t3.aps_work_order_id=t5.id\n" +
            "            ${ew.customSqlSegment}")
    List<WorkshopPlatePartResVo> listPlatePart(@Param(Constants.WRAPPER) QueryWrapper<SinoCamReceiveNestPart> query);


    @Select(" select awd.id, ao.order_code, ao.construct_no, awo.work_order_code, ao.seq splitNum, task.status, awd.phase_seq, awd.aps_work_order_id, " +
            " awd.mater_no materCode, awd.mater_name, part.part_map_url drawing, part.sorting_count finishCount, part.quantity totalCount, " +
            " part.width, part.length, part.thickness, part.net_weight weight, " +
            " plate.stock_list_no plateCode, plate.materiel_name plateName, plate.nest_id , plate.texture plateTexture, plate.norm plateThickness " +
            " from tb_third_sinocam_receive_nest_part part " +
            " left join tb_mes_work_detail_task task on task.mater_code = part.plate_code and task.phase_code='FJ' " +
            " left join tb_third_sinocam_receive_nest_plate plate on plate.stock_list_no =  part.plate_code" +
            " left join tb_aps_work_detail awd on awd.id = part.nest_plan_id " +
            " left join tb_aps_work_order awo on awd.aps_work_order_id = awo.id" +
            " left join tb_aps_order ao on awd.aps_order_id = ao.id" +
            " ${ew.customSqlSegment} "
    )
    Page<PlatePartListResVo> platePartList(Page<Object> page, @Param(Constants.WRAPPER)  QueryWrapper<SinoCamReceiveNestPart> query);
    @Select("    SELECT mwr.id,mwr.STATUS,awd.phase_seq,awd.aps_work_order_id,mwr.mater_code materCode,mwr.mater_name AS materName,mwr.total_count finishCount,\n" +
            "    ao.order_code,ao.construct_no,awo.work_order_code,ao.seq splitNum,\n" +
            "   plate.stock_list_no plateCode,plate.materiel_name plateName,plate.nest_id,plate.texture plateTexture,plate.norm plateThickness,\n" +
            "   part.png_url drawing,part.width,part.length,part.thickness,part.net_weight weight \n" +
            " FROM tb_mes_work_detail_task mwr\n" +
            " LEFT JOIN tb_order_part  part ON part.plm_id = mwr.mater_code\n" +
            " LEFT JOIN tb_third_sinocam_receive_nest_plate plate ON mwr.nest_plate_id = plate.id\n" +
            " LEFT JOIN tb_aps_work_detail awd ON awd.id = mwr.aps_work_detail_id\n" +
            " LEFT JOIN tb_aps_work_order awo ON awd.aps_work_order_id = awo.id\n" +
            "  LEFT JOIN tb_aps_order ao ON awd.aps_order_id = ao.id " +
            " ${ew.customSqlSegment} "
    )
    Page<PlatePartListResVo> platePartEndList(Page<Object> page, @Param(Constants.WRAPPER)  QueryWrapper<SinoCamReceiveNestPart> query);


    @Select(" select part.* " +
            " from tb_third_sinocam_receive_nest_part part " +
            " left join tb_aps_work_detail awd on awd.id = part.nest_plan_id " +
            " ${ew.customSqlSegment} ")
    List<SinoCamReceiveNestPart> findByWorkOrderId(@Param(Constants.WRAPPER)  QueryWrapper<SinoCamReceiveNestPart> query);

    @Select(" select part.id id, o.serial_code serialCode, ao.seq pkgCode, bom.drawing drawing, bom.plm_id plmId, part.quantity nestCount, " +
            " bom.standards spec, part.thickness thickness, part.length length, part.net_weight weight," +
            " part.width width, part.part_map_url partMapUrl, part.area area ,awd.plan_start_date_time planStartDateTime" +
            " from tb_third_sinocam_receive_nest_part part " +
            " left join tb_aps_work_detail awd on awd.id = part.nest_plan_id " +
            " left join tb_aps_work_order awo on awd.aps_work_order_id = awo.id " +
            " left join tb_order_bom bom on awo.bom_id = bom.id " +
            " left join tb_aps_order ao on awd.aps_order_id = ao.id " +
            " left join tb_order o on awd.order_id = o.id " +
            " ${ew.customSqlSegment} ")
    List<ReportProductionNestPartResVo> productionNestPart(@Param(Constants.WRAPPER) QueryWrapper<SinoCamReceiveNestPart> query);

    @Select(" select sum(part.quantity) " +
            " from tb_third_sinocam_receive_nest_part part " +
            " left join tb_aps_work_detail awd on part.nest_plan_id = awd.id " +
            " left join tb_aps_work_order awo on awd.aps_work_order_id = awo.id " +
            " where awo.bom_id = #{id}")
    Long selectTotalCountByBomId(@Param("id") Integer id);


    @Select(" select sum(part.quantity) " +
            " from tb_third_sinocam_receive_nest_part part " +
            " left join tb_aps_work_detail awd on part.nest_plan_id = awd.id " +
            " left join tb_aps_work_order awo on awd.aps_work_order_id = awo.id " +
            " where awo.id = #{id}")
    Long selectTotalCountById(@Param("id") Integer id);

    @Select(" select part_id, part.id id, nest.nest_id nestId, nest.option_id groupcode, part.plate_code plateCode,  " +
            " part.length , part.width, part.thickness, part.quantity nestCount, nest.need_date planStartTime, nest.nester createUserCode, nest.nest_date createDateTime " +
            " from tb_third_sinocam_receive_nest_part part " +
            " left join tb_third_sinocam_receive_nest_nest nest on part.receive_nest_id = nest.receive_nest_id " +
            " ${ew.customSqlSegment} ")
    Page<ReportProductionPartNestResVo> productionPartNest(@Param(Constants.WRAPPER) QueryWrapper<SinoCamReceiveNestPart> query, Page<SinoCamReceiveNestPart> page);

    @Select(" select nest.nest_id nestId, part.plate_code plateCode,plate.png_url pngUrl ,nest.option_id groupCode, part.quantity nestCount, ob.standards spec, part.thickness thickness, part.length length, part.width width, awd.plan_start_date_time planStartTime, nest.nester createUserCode, nest.create_date_time createDateTime, nest.nester modifyUserCode, nest.create_date_time modifyDateTime " +
            " from tb_third_sinocam_receive_nest_part part " +
            " left join tb_third_sinocam_receive_nest_nest nest on part.receive_nest_id = nest.receive_nest_id " +
            " left join tb_third_sinocam_receive_nest_plate plate on part.receive_nest_id = plate.receive_nest_id " +
            " left join tb_aps_work_detail awd on part.nest_plan_id = awd.id " +
            " left join tb_aps_work_order awo on awd.aps_work_order_id = awo.id " +
            " left join tb_order_bom ob on awo.bom_id = ob.id " +
            " ${ew.customSqlSegment} ")
    Page<ReportProductionOrderNestResVo> productionOrderNest(@Param(Constants.WRAPPER) QueryWrapper<SinoCamReceiveNestPart> query, Page<SinoCamReceiveNestPart> page);

    @Select("DELETE FROM tb_third_sinocam_receive_nest_part WHERE sino_cam_id=#{sId}")
    void deletePreOrder(@Param("sId") Integer sId);

    @Select("SELECT " +
            "  n.nest_id " +
            "FROM " +
            "  tb_third_sinocam_receive_nest_part p " +
            "  LEFT JOIN tb_third_sinocam_receive_nest_nest n ON p.receive_nest_id = n.receive_nest_id " +
            " ${ew.customSqlSegment} ")
    List<String> applyDrawUpdate(@Param(Constants.WRAPPER) QueryWrapper<SinoCamReceiveNestPart> query);

    @Select(" SELECT t1.* " +
            " FROM tb_third_sinocam_receive_nest_part t1 " +
            " INNER JOIN ( " +
            "    SELECT rectangle_area, MAX(id) as max_id " +
            "    FROM tb_third_sinocam_receive_nest_part " +
            "    GROUP BY rectangle_area " +
            " ) t2 ON t1.rectangle_area = t2.rectangle_area AND t1.id = t2.max_id " +
            " ${ew.customSqlSegment}")
    List<SinoCamReceiveNestPart> maxRectangleArea(@Param(Constants.WRAPPER) LambdaQueryWrapper<SinoCamReceiveNestPart> query11);

    @Select("DELETE FROM tb_third_sinocam_receive_nest_part WHERE id = #{id}")
    void deleteMesNest(@Param("id") Integer id);

    @Select("DELETE FROM tb_third_sinocam_receive_nest_part WHERE plate_code = #{plateCode}")
    void deleteMesNestWithCode(@Param("plateCode") String plateCode);

    @Select("SELECT o.construct_no AS constructNo, o.order_code AS orderNo, awo.work_order_code AS workOrderNo, awop.work_order_code AS parWorkOrderNo, awo.plm_id AS plmId, " +
            "awop.plm_id AS parPlmid, awo.mater_name AS partName, part.quantity AS partCount, part.plate_code AS plateNo, awo.erp_code AS itemNo, pushplan.processing_no AS packageNo " +
            "FROM tb_third_sinocam_receive_nest_part part LEFT JOIN tb_third_sinocam_pushplan pushplan ON part.nest_plan_id = pushplan.nest_plan_id LEFT JOIN tb_aps_work_detail awd ON part.nest_plan_id = awd.id " +
            "LEFT JOIN tb_aps_work_order awo ON awd.aps_work_order_id = awo.id LEFT JOIN tb_aps_work_order awop ON awo.pid = awop.id LEFT JOIN tb_order o ON awd.order_id = o.id " +
            "WHERE part.plate_code = #{plateCode} ")
    List<PlatePartReqVo> getDataToQms(@Param("plateCode") String plateCode);

    @Select("SELECT pp.product_id " +
            "FROM tb_third_sinocam_receive_nest_part p " +
            "LEFT JOIN tb_third_sinocam_pushplan pp ON p.nest_plan_id = pp.nest_plan_id " +
            "WHERE pp.product_id LIKE 'Z%' " +
            "AND p.plate_code = #{plateCode} " +
            "LIMIT 1 ")
    String findConstructNo(@Param("plateCode") String plateCode);

}
